{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Samples per second based on minimum elapsed time: 10.28\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "with open(\"tune_llama2_7b_gbs128_iter1200.log\", \"r\", encoding=\"utf-8\") as file:\n",
    "    lines = file.readlines()\n",
    "\n",
    "min_elapsed_time_s = float(\"inf\")  # 初始化为正无穷\n",
    "total_samples_per_s = 0\n",
    "\n",
    "pattern = re.compile(\n",
    "    r\"iteration\\s+(\\d+)/\\s+\\d+\\s+\\|\\s+consumed samples:\\s+(\\d+)\\s+\\|\\s+elapsed time per iteration \\(ms\\):\\s+([\\d.]+)\"\n",
    ")\n",
    "\n",
    "for log in lines:\n",
    "    if \"elapsed time per iteration\" in log:\n",
    "        match = pattern.search(log)\n",
    "        if match:\n",
    "            iteration = int(match.group(1))\n",
    "            consumed_samples = int(match.group(2))\n",
    "            elapsed_time_ms = float(match.group(3))\n",
    "\n",
    "            elapsed_time_s = elapsed_time_ms / 1000  # 转换为秒\n",
    "\n",
    "            # 更新最小值\n",
    "            if elapsed_time_s < min_elapsed_time_s:\n",
    "                min_elapsed_time_s = elapsed_time_s\n",
    "\n",
    "# 计算最小的 elapsed_time_s 对应的 samples_per_s\n",
    "samples_per_s = 128 / 4 / min_elapsed_time_s  # 计算 samples/s\n",
    "print(f\"Samples per second based on minimum elapsed time: {samples_per_s:.2f}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pytorch",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.20"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
